package com.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.stereotype.Repository;

import com.bean.Rol;
import com.dao.RecursosDao;

@Repository("RolDaoImpl")
public class RolDaoImpl implements RecursosDao {
	Logger log	= Logger.getLogger(RolDaoImpl.class);

	@Autowired
	private SessionFactory	sessionFactory;
	
	@Override
	public Object buscaRegistroPorId(Object registro) {
		log.debug("begin");
		Rol rol		= (Rol) registro;
		Session session		= SessionFactoryUtils.getSession(sessionFactory, true);
		Criteria criterio	= session.createCriteria(Rol.class);
		log.debug("rol: "+rol.getRol());
		log.debug("criterio de busqueda: "+Restrictions.eq("rol", rol.getRol()));
		criterio.add(Restrictions.eq("rol", rol.getRol()));
		log.debug("finally");
		return criterio.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> List<T> listaDeRegistros() {
		log.debug("begin");
		Session session		= SessionFactoryUtils.getSession(sessionFactory, true);
		Criteria criterio	= session.createCriteria(Rol.class);
		log.debug("finally");
		return criterio.list();
	}
}